package com.timer.taskRule;

import javax.crypto.*;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

public class DbPwdEnc {

	public static void main(String[] args) {
		encrypt();
		decrypt();
	}

	//加密
	public static String encrypt(){
		SecretKeyFactory keyFactory;
		SecretKey secretKey = null;
		byte[] encipheredData = null;
		try {
			keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
			// 设置PBE密钥参数
			PBEKeySpec keySpec = new PBEKeySpec("1".toCharArray());
			// 生成密钥
			secretKey = keyFactory.generateSecret(keySpec);


			PBEParameterSpec parameterSpec = new PBEParameterSpec("63293188".getBytes(), 1000);
			Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
			cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
			encipheredData = cipher.doFinal("PLDRPT".getBytes());

			System.out.println(bytesToHexString(encipheredData));

		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		}
		return bytesToHexString(encipheredData);
	}


	public static String decrypt(){
// 实例化使用的算法
		SecretKeyFactory keyFactory;
		SecretKey secretKey = null;
		try {
			keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
			// 设置PBE密钥参数
			PBEKeySpec keySpec = new PBEKeySpec("1".toCharArray());
			// 生成密钥
			secretKey = keyFactory.generateSecret(keySpec);

			byte[] passDec = null;
			PBEParameterSpec parameterSpec = new PBEParameterSpec("63293188".getBytes(), 1000);

			Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");

			cipher.init(Cipher.DECRYPT_MODE, secretKey, parameterSpec);

			passDec = cipher.doFinal(hexStringToBytes("1fe8e1437ac3f5eb"));

			System.out.println(passDec==null?"":(new String(passDec)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}
}

